package cn.lsh.mapper;

import cn.lsh.hbase.HbaseUtil;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.Text;

import java.io.IOException;

public class HbaseMaxMapper extends TableMapper<Text, Text> {
	Text mKey = new Text();

	Text mValue = new Text();

	@Override
	protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
		String rowKey = Bytes.toString(value.getRow());
		if (rowKey.startsWith("u")) {
			mKey.set("user");
		} else if (rowKey.startsWith("i")) {
			mKey.set("item");
		} else {
			return;
		}
		mValue.set(rowKey + ":" + HbaseUtil.getCellValue(value, "cf", "count"));
		context.write(mKey, mValue);
	}
}
